﻿# 检查管理员权限
$isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if (-not $isAdmin) {
    Start-Process PowerShell -Verb RunAs "-NoProfile -ExecutionPolicy Bypass -Command `"cd '$pwd'; & '$PSCommandPath';`""
    exit
}

# 设置颜色
$host.UI.RawUI.BackgroundColor = "Black"
$host.UI.RawUI.ForegroundColor = "White"
Clear-Host

# 显示欢迎信息
Write-Host "`n"
Write-Host "███╗   ███╗ ██████╗     ██████╗██╗      ██████╗ ██╗   ██╗██████╗" -ForegroundColor Cyan
Write-Host "████╗ ████║██╔════╝    ██║     ██║     ██╔═══██╗██║   ██║██╔══██╗" -ForegroundColor Cyan
Write-Host "██╔████╔██║██║         ██║     ██║     ██║   ██║██║   ██║██║  ██║" -ForegroundColor Cyan
Write-Host "██║╚██╔╝██║██║         ██║     ██║     ██║   ██║██║   ██║██║  ██║" -ForegroundColor Cyan
Write-Host "██║ ╚═╝ ██║╚██████╗    ███████╗███████╗╚██████╔╝╚██████╔╝██████╔╝" -ForegroundColor Cyan
Write-Host "╚═╝     ╚═╝ ╚═════╝    ╚══════╝╚══════╝ ╚═════╝  ╚═════╝ ╚═════╝" -ForegroundColor Cyan
Write-Host "`n"
Write-Host "MCloud 智能机器人集成安装系统 (Windows版)" -ForegroundColor Blue

# 显示版权信息
Write-Host "`n版权所有：MC云-小馒头" -ForegroundColor Yellow
Write-Host "作者：小馒头" -ForegroundColor Yellow
Write-Host "联系QQ：1317957427" -ForegroundColor Yellow
Write-Host "QQ群：112406641" -ForegroundColor Yellow

# 显示赞助商信息
Write-Host "`n====== 赞助商信息 ======" -ForegroundColor Cyan
Write-Host "▶ MC云" 
Write-Host "  2核4G高性能云电脑每月只要5.99￥ 24小时挂机没有任何问题！" -ForegroundColor Yellow
Write-Host "  https://idc.stay33.cn/cart" -ForegroundColor Yellow
Write-Host "========================" -ForegroundColor Cyan

# 倒计时
Write-Host "`n即将开始安装..." -ForegroundColor Yellow
for ($i = 5; $i -ge 1; $i--) {
    Write-Host "`r$i 秒后开始安装！" -ForegroundColor Yellow -NoNewline
    Start-Sleep -Seconds 1
}
Write-Host "`n"

# 设置桌面路径
$desktopPath = [Environment]::GetFolderPath("Desktop")
$installPath = Join-Path $desktopPath "MCloudInstall"

# 创建安装目录
if (-not (Test-Path $installPath)) {
    New-Item -Path $installPath -ItemType Directory | Out-Null
}

# 下载README.md到桌面
Write-Host "正在下载README.md到桌面..." -ForegroundColor Cyan
$readmeUrl = "https://gitee.com/mc_cloud/mccloud_bot/raw/master/windows/README.md"
Invoke-WebRequest -Uri $readmeUrl -OutFile (Join-Path $desktopPath "MCCloud_README.md")
Write-Host "✓ README.md 下载完成" -ForegroundColor Green

# 创建继续安装的脚本
$continueScriptPath = Join-Path $installPath "MCCloud_Continue.ps1"
$continueScript = @'
# 检查管理员权限
$isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if (-not $isAdmin) {
    Start-Process PowerShell -Verb RunAs "-NoProfile -ExecutionPolicy Bypass -Command `"cd '$pwd'; & '$PSCommandPath';`""
    exit
}

Clear-Host
Write-Host "MCloud 安装继续进行中..." -ForegroundColor Cyan

# 安装Ubuntu
Write-Host "`n[1/5] 正在安装 Ubuntu..." -ForegroundColor Cyan
wsl --install -d Ubuntu
Write-Host "✓ Ubuntu 安装命令已执行" -ForegroundColor Green

# 运行 wsl -d Ubuntu
Write-Host "`n[2/5] 正在启动 Ubuntu WSL..." -ForegroundColor Cyan
wsl -d Ubuntu -e bash -c "echo '正在配置 Ubuntu 环境...'"

# 设置root密码
Write-Host "`n[3/5] 正在设置 root 密码..." -ForegroundColor Cyan
wsl -d Ubuntu -e bash -c "echo '123456\n123456' | sudo passwd root"
Write-Host "✓ root 密码已设置为: 123456" -ForegroundColor Green

# 更新并安装SSH服务
Write-Host "`n[4/5] 正在更新系统并安装SSH服务..." -ForegroundColor Cyan
wsl -d Ubuntu -e bash -c "sudo apt update && sudo apt install -y openssh-server"
Write-Host "✓ 系统更新和SSH安装完成" -ForegroundColor Green

# 切换到root用户并执行安装脚本
Write-Host "`n[5/5] 正在执行 MCloud 安装脚本..." -ForegroundColor Yellow
Write-Host "如果安装过程中出现网络问题，请确保您已开启代理或VPN。" -ForegroundColor Red
wsl -d Ubuntu -e bash -c "sudo -i -c 'wget -O - https://gitee.com/mc_cloud/mccloud_bot/raw/master/mccloud_install_u.sh | bash'"

Write-Host "`n安装完成！请查看上面的信息获取访问地址和凭据。" -ForegroundColor Green
Write-Host "按任意键退出..." -ForegroundColor Yellow
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
'@

Set-Content -Path $continueScriptPath -Value $continueScript

# 创建安装脚本快捷方式
$WshShell = New-Object -ComObject WScript.Shell
$shortcutPath = Join-Path $desktopPath "继续MCloud安装.lnk"
$shortcut = $WshShell.CreateShortcut($shortcutPath)
$shortcut.TargetPath = "powershell.exe"
$shortcut.Arguments = "-ExecutionPolicy Bypass -File `"$continueScriptPath`""
$shortcut.WorkingDirectory = $installPath
$shortcut.IconLocation = "powershell.exe,0"
$shortcut.Save()

# 开始WSL安装
Write-Host "`n[1/1] 正在安装 WSL..." -ForegroundColor Cyan
Write-Host "如果安装过程中出现网络问题，请确保您已开启代理或VPN。" -ForegroundColor Red
wsl --install
Write-Host "✓ WSL 安装命令已执行" -ForegroundColor Green

Write-Host "`n安装第一阶段完成" -ForegroundColor Green
Write-Host "请重启计算机，然后双击桌面上的'继续MCloud安装'快捷方式完成安装。" -ForegroundColor Yellow
Write-Host "`n如果安装过程中出现问题，请检查您的网络连接并确保代理或VPN已开启。" -ForegroundColor Red

# 在最后显示赞助商信息
Write-Host "`n====== 赞助商信息 ======" -ForegroundColor Cyan
Write-Host "▶ MC云" 
Write-Host "  2核4G高性能云电脑每月只要5.99￥ 24小时挂机没有任何问题！" -ForegroundColor Green
Write-Host "  https://idc.stay33.cn/cart" -ForegroundColor Green
Write-Host "========================" -ForegroundColor Cyan

Write-Host "`n按任意键退出..." -ForegroundColor Yellow
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")